package com.eupmt.lab3.persistence.ibatis.dao;

import com.eupmt.lab3.persistence.ibatis.dao.ibator.IbatorRolDAOImpl;
import com.eupmt.lab3.domain.Rol;
import com.eupmt.lab3.exceptions.RolJaExisteixException;
import com.eupmt.lab3.persistence.ibatis.PersistenceManager;
import com.eupmt.lab3.utils.Log;
import static com.eupmt.lab3.utils.Constants.*;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RolDAOImpl extends IbatorRolDAOImpl implements RolDAO {

    private SqlMapClient sqlMapClient;
    private Log log = null;

    //Constructors
    public RolDAOImpl() {
        super();
        sqlMapClient = PersistenceManager.getInstance().getSqlMapClient();
        log = new Log();
    }

    //Metodes
    public void saveRol(Rol rol) throws Exception {
        log.infoLog("Rol a fer save: " + rol.getIdrol());

        if (rol.getIdrol() == UNDEFINED) {
            insertRol(rol);
        } else {
            updateRol(rol);
        }
    }

    public void insertRol(Rol rol) throws Exception {

        try {
            sqlMapClient.insert("rol.insert", rol);
        } catch (SQLException sqle) {
            if (sqle.getErrorCode() == ROL_JA_EXISTEIX) {
                throw new RolJaExisteixException(sqle.getMessage());
            }
            log.infoLog("Insert SQLErrorCode:" + sqle.getErrorCode() + ":");
        }
    }

    public void updateRol(Rol rol) throws SQLException {
        sqlMapClient.update("rol.update", rol);
    }

    public void deleteRol(Rol rol) throws SQLException {
        sqlMapClient.delete("rol.delete", rol);
    }

    public Rol getRolById(int idRol) throws Exception {
        Rol rol = new Rol();
        rol.setIdrol(idRol);

        Rol rolPersistence = null;
        try {
            rolPersistence = (Rol) sqlMapClient.queryForObject("rol.getRolById", rol);
        } catch (MySQLIntegrityConstraintViolationException sqle) {
            log.infoLog("SQLErrorCode: " + sqle.getErrorCode());
        }
        return rolPersistence;
    }

    public Rol getRolByNom(String nom) throws SQLException {
        Rol rol = new Rol();
        rol.setNom(nom);

        Rol rolPersistence = null;

        try {
            rolPersistence = (Rol) sqlMapClient.queryForObject("rol.getRolByNom", rol);
        } catch (MySQLIntegrityConstraintViolationException sqle) {
            log.infoLog("SQLErrorCode: " + sqle.getErrorCode());
        }
        return rolPersistence;
    }

    public List<Rol> getAllRols() throws Exception {
        Rol rol = new Rol();

        List<Rol> rolPersistence = new ArrayList<Rol>();

        try {
            rolPersistence = (List<Rol>) sqlMapClient.queryForList("rol.getAllRols", rol);

        } catch (MySQLIntegrityConstraintViolationException sqle) {
            log.infoLog("SQLErrorCode: " + sqle.getErrorCode());
        }
        return rolPersistence;
    }

    public List<Rol> getRolsLikeNom(String nom) throws Exception {
        Rol rol = new Rol();
        rol.setNom(nom);

        List<Rol> rolPersistence = new ArrayList<Rol>();

        try {
            rolPersistence = (List<Rol>) sqlMapClient.queryForList("rol.getRolsLikeNom", rol);

        } catch (MySQLIntegrityConstraintViolationException sqle) {
            log.infoLog("SQLErrorCode: " + sqle.getErrorCode());
        }
        return rolPersistence;
    }
}
